﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /// <summary>
    /// A control to insert an image
    /// The control is moveable, and sizeable
    /// </summary>
    public partial class ImagePositionningBox : UserControl
    {
        Point InitialMouseClick;
        Rectangle InitialRectangle;
        bool _MouseDown;
        BaseTool CurrentTool;

        /// <summary>
        /// A control to insert an image
        /// The control is moveable, and sizeable
        /// </summary>
        /// <param name="Img">The image to insert</param>
        /// <param name="CurrentTool">The image insertion tool, so the control can send a message to the tool to tell the position is ok</param>
        public ImagePositionningBox(Bitmap Img, BaseTool CurrentTool)
        {
            this.CurrentTool = CurrentTool;

            InitializeComponent();

            RectangleD PictureSourceRectangle = new RectangleD(0, 0, Img.Width, Img.Height);
            RectangleD PictureDestinationRectangle = Tools.FitRectangleIntoRectangle(this.Bounds, PictureSourceRectangle);
            this.Width = (int)PictureDestinationRectangle.Width;
            this.Height = (int)PictureDestinationRectangle.Height;

            PictureBox_Image.Image = Img;
           
            RearrangeComponents();

            PictureBox_Image.MouseDown += new MouseEventHandler(Action_MouseDown);
            PictureBox_Image.MouseUp += new MouseEventHandler(Action_MouseUp);
            PictureBox_Image.MouseMove += new MouseEventHandler(ImagePositionningBox_MouseMove);

            Button_SizeBoth.MouseDown += new MouseEventHandler(Action_MouseDown);
            Button_SizeBoth.MouseUp += new MouseEventHandler(Action_MouseUp);
            Button_SizeBoth.MouseMove += new MouseEventHandler(Button_SizeBoth_MouseMove);

            Button_SizeHeight.MouseDown += new MouseEventHandler(Action_MouseDown);
            Button_SizeHeight.MouseUp += new MouseEventHandler(Action_MouseUp);
            Button_SizeHeight.MouseMove += new MouseEventHandler(Button_SizeHeight_MouseMove);

            Button_SizeWidth.MouseDown += new MouseEventHandler(Action_MouseDown);
            Button_SizeWidth.MouseUp += new MouseEventHandler(Action_MouseUp);
            Button_SizeWidth.MouseMove += new MouseEventHandler(Button_SizeWidth_MouseMove);
        }

        void Button_SizeWidth_MouseMove(object sender, MouseEventArgs e)
        {
            if (_MouseDown)
            {
                this.Width = InitialRectangle.Width + PointToScreen(e.Location).X - InitialMouseClick.X;

                if (CheckBox_AspectRatio.Checked)
                {
                    this.Height = (int)(this.Width * (float)InitialRectangle.Height / InitialRectangle.Width);
                }

                ((BaseGeoDisplayBox)this.Parent).Refresh();
                this.Refresh();
            }
        }

        void Action_MouseUp(object sender, MouseEventArgs e)
        {
            _MouseDown = false;
            RearrangeComponents();
            ((BaseGeoDisplayBox)this.Parent).Viewport.DoubleBuffereEnabled = false;
        }

        void Action_MouseDown(object sender, MouseEventArgs e)
        {
            _MouseDown = true;
            InitialMouseClick = PointToScreen(e.Location);
            InitialRectangle = this.Bounds;
            ((BaseGeoDisplayBox)this.Parent).Viewport.DoubleBuffereEnabled = true;
        }

        void Button_SizeHeight_MouseMove(object sender, MouseEventArgs e)
        {
            if (_MouseDown)
            {
                this.Height = InitialRectangle.Height + PointToScreen(e.Location).Y - InitialMouseClick.Y;

                if (CheckBox_AspectRatio.Checked)
                {
                    this.Width = (int)(this.Height * (float)InitialRectangle.Width / InitialRectangle.Height);
                }

                ((BaseGeoDisplayBox)this.Parent).Refresh();
                this.Refresh();
            }
        }

        void Button_SizeBoth_MouseMove(object sender, MouseEventArgs e)
        {
            if (_MouseDown)
            {
                this.Width = InitialRectangle.Width + PointToScreen(e.Location).X - InitialMouseClick.X;

                if (CheckBox_AspectRatio.Checked)
                {
                    this.Height = (int)(this.Width * (float)InitialRectangle.Height / InitialRectangle.Width);
                }
                else
                {
                    this.Height = InitialRectangle.Height + PointToScreen(e.Location).Y - InitialMouseClick.Y;
                }

                ((BaseGeoDisplayBox)this.Parent).Refresh();
                this.Refresh();
            }
        }

        void ImagePositionningBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (_MouseDown)
            {
                this.Left = InitialRectangle.X + PointToScreen(e.Location).X - InitialMouseClick.X;
                this.Top = InitialRectangle.Y + PointToScreen(e.Location).Y - InitialMouseClick.Y;
            }
        }

        void RearrangeComponents()
        {
            Button_SizeWidth.Top = (this.Height - Button_SizeWidth.Height) / 2;
            Button_SizeWidth.Left = this.Width - Button_SizeWidth.Width;

            Button_SizeBoth.Top = this.Height - Button_SizeBoth.Height;
            Button_SizeBoth.Left = this.Width - Button_SizeBoth.Width;

            Button_SizeHeight.Top = this.Height - Button_SizeHeight.Height;
            Button_SizeHeight.Left = (this.Width - Button_SizeHeight.Width) / 2;

            CheckBox_AspectRatio.Left = this.Width - CheckBox_AspectRatio.Width;
        }

        private void Button_Accept_Click(object sender, EventArgs e)
        {
            CurrentTool.TextEntry("OK");
        }
    }
}
